<?php
class ml_biz_suggest_tag {
    
    private $rds;
    
    function __construct(){
        $this->rds = new ml_model_rdsSuggest();
    }
    
    
    /**
     * 首字母+英文+其他
     * Enter description here ...
     * @param unknown_type $tag
     * @param unknown_type $tag_id
     */
    private  function push_tag_index_byFirstLetter($tag, $tag_id){
        $spell = ml_tool_chineseSpell::getFirstLetter(trim($tag),'');
        
        $length = strlen($spell);
        for ($i=1; $i<=$length; $i++){
            $key = substr($spell, 0, $i);
            
            //空格不敏感，递归
            if (substr($key, -1)== ' ') {
                $spellnospace = substr_replace($spell,strrchr($key, ' '), '');
                $this->push_tag_index_byFirstLetter($spellnospace, $tag_id);
            }
            
            
            $key = trim($key);
            $re = $this->cal_tag_score($key, $tag_id);
        }
    }
    
    /**
     * 拼音+英文+其他
     * Enter description here ...
     * @param unknown_type $tag
     * @param unknown_type $tag_id
     */
    private function push_tag_index_byFullLetter($tag, $tag_id){
        $spell = ml_tool_chineseSpell::getFullSpell(trim($tag),'');
        
        $length = strlen($spell);
        for ($i=1; $i<=$length; $i++){
            $key = substr($spell, 0, $i);
        
            //空格不敏感，递归
            if (substr($key, -1)== ' ') {
                $spellnospace = substr_replace($spell,strrchr($key, ' '), '');
                $this->push_tag_index_byFullLetter($spellnospace, $tag_id);
            }
            
            $key = trim($key);
            $re =$this->cal_tag_score($key, $tag_id);
        }
    }
    
    /**
     * 汉字+英文+其他
     * Enter description here ...
     * @param unknown_type $tag
     * @param unknown_type $tag_id
     */
    private function push_tag_index_byChinese($tag, $tag_id){
        $spell = trim($tag);
        
        $length = Tool_string::count_all_character($spell);
        for ($i=1; $i<=$length; $i++){
            $key = Tool_string::substr_by_charater($spell, 0, $i);
            
            //空格不敏感，递归
            if (substr($key, -1)== ' ') {
                $spellnospace = substr_replace($spell,strrchr($key, ' '), '');
                $this->push_tag_index_byChinese($spellnospace, $tag_id);
            }
            
            $key = trim($key);
            $re = $this->cal_tag_score($key, $tag_id);
        }
    }
    
    /**
     * tag初始化redis
     * Enter description here ...
     * @param unknown_type $tag
     * @param unknown_type $tag_id
     * @param unknown_type $score
     */
    public function init_tag_redis($tag, $tag_id, $score=0){
        $tag = trim($tag);

        $re = $this->rds->setSuggestTagsData($tag_id, array('tag'=>$tag));
        
    //    if (!$re){
    //        return false;
    //    }
        
        //空格不敏感
        //大小写不敏感
        $tag = strtolower($tag);
        
        $re = $this->push_tag_index_byChinese($tag, $tag_id);
        $re = $this->push_tag_index_byFirstLetter($tag, $tag_id);
        $re = $this->push_tag_index_byFullLetter($tag, $tag_id);
        
    }
    
    private function deal_tag_space(){
        
    }
    
    private function cal_tag_score($mainkey, $tag_id){
        
        $rs = $this->rds->getSuggestTagsIndex($mainkey);
        
        $rs[] = $tag_id;
        $rs = array_unique($rs);
        
        $arr = $this->get_tag_data($rs);
        
        foreach ($arr as $key => $value){
            $value = json_decode($value, true);
            $value['tag'] = strtolower($value['tag']);
            $a[$key] = ml_tool_chineseSpell::U2_Utf8_Gb($value['tag']);
        }
        
        asort($a, SORT_STRING);
        
        foreach ($a as $key =>$value){
            $b[] = $key;
        }
        //var_dump($b);
        $re =$this->rds->setSuggestTagsIndex($mainkey, $b);
    }
    
    
    
    private function get_tag_id($tag){
    }
    
    public  function get_tag_index($key){
        
        $rs = $this->rds->getSuggestTagsIndex($key);
        return $rs;
    }
    
    public function get_tag_data($aTagid){
        
        $re = $this->rds->getMSuggestTagsData($aTagid);
        $rs = $this->rds->get_data();
        return $rs;
    }
    
    public function get_suggest_tag($key){
        $key = trim($key);
        
        $aTagid = $this->get_tag_index($key);
        $data = $this->get_tag_data($aTagid);
        foreach($data as $j){
            $a[] = json_decode($j, true);
        };
        return $a;
    }
}